package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/***
 * 功能描述:定时任务配置
 * @author v_kpkli
 * @date 2021/12/18
 */
@Configuration
@EnableScheduling
public class SpringScheduleConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
//        scheduledTaskRegistrar.setScheduler(taskExecutor());
    }

    @Bean
    public ScheduledExecutorService taskScheduler() {
        //设置需要并行执行的任务数量
        int corePoolSize = 20;
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }

    // 多线程配置,报错了
//    @Bean
//    public ThreadPoolTaskExecutor taskExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
//        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors()*5);
//        executor.setThreadNamePrefix("task-executor-");
//        return executor;
//    }

}